﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
    public class ZLog
    {
        public static string LogFileName
        {
            get
            {
                return CConfig.ReadAppSettings("LogPath") + DateTime.Now.ToString("yyyyMMdd") + ".log";
            }
        }

        public static string ErrorLogFileName
        {
            get
            {
                return CConfig.ReadAppSettings("LogPath") + DateTime.Now.ToString("yyyyMMdd") + "_err.log";
            }
        }

        public static string FanLiErrorLogFileName
        {
            get
            {
                return CConfig.ReadAppSettings("LogPath") + DateTime.Now.ToString("yyyyMMdd") + "_FanLiErr.log";
            }
        }

        public static void SaveError(Exception ex, string method, string ip, string url)
        {
            if (ZConfig.IFLogError)
            {
                string errorMessage = "DateTime:" + DateTime.Now.ToLongTimeString() + "\r\n";
                errorMessage += "URI:" + url + "\r\n";
                errorMessage += "MyMethod:" + method + "\r\n";
                errorMessage += "Method:" + ex.TargetSite.Name + "\r\n";
                errorMessage += "IP:" + ip + "\r\n";
                errorMessage += "Error:" + ex.Message + "\r\n";
                errorMessage += "Source:" + ex.Source + "\r\n";
                errorMessage += "StackTrace:" + ex.StackTrace.Replace("\r\n", "") + "\r\n\r\n";
                ZIO.Save(ZLog.ErrorLogFileName, errorMessage);
            }
        }

        public static void SaveError(Exception ex, string method, string ip, string url, string filePath)
        {
            if (ZConfig.IFLogError)
            {
                string errorMessage = "DateTime:" + DateTime.Now.ToLongTimeString() + "\r\n";
                errorMessage += "URI:" + url + "\r\n";
                errorMessage += "MyMethod:" + method + "\r\n";
                errorMessage += "Method:" + ex.TargetSite.Name + "\r\n";
                errorMessage += "IP:" + ip + "\r\n";
                errorMessage += "Error:" + ex.Message + "\r\n";
                errorMessage += "Source:" + ex.Source + "\r\n";
                errorMessage += "StackTrace:" + ex.StackTrace.Replace("\r\n", "") + "\r\n\r\n";
                ZIO.Save(filePath, errorMessage);
            }
        }

        public static void SaveLog(string local, string client, string result)
        {
            if (ZConfig.IFLogError)
            {
                string errorMessage = "DateTime:" + DateTime.Now.ToLongTimeString() + "\r\n";               
                errorMessage += "Client:" + local + "\r\n";
                errorMessage += "Error:" + client + "\r\n";
                errorMessage += "Result:" + result + "\r\n\r\n";
                ZIO.Save(ZLog.LogFileName, errorMessage);
            }
        }
    }
}
